<?php
/**
* TorTags component for Joomla 1.6, Joomla 1.7, Joomla 2.5
* @package TorTags
* @author Zjmainstay
* @Copyright Copyright (C) Tormix, www.tormix.com
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );

class plgSystemAjaxlogout extends JPlugin
{
	function __construct(& $subject, $config)
	{
		parent::__construct($subject, $config);
		$this->loadLanguage('plg_system_ajaxlogout');
		$doc = JFactory::getDocument();

		//Include jQuery library
		if($this->params->get('jquery',null)){
			$doc->addScript(JURI::root().'plugins/system/ajaxlogout/core/jquery-1.8.2.min.js');
		}

		//Add click listener
		$class = $this->params->get('class','logout');
		$js = '
		(function($){
			$(document).ready(function(){
				$(".'.$class.'").click(function(){
					if(typeof console.log !="undefined") console.log("Ajax logout.");
					$.getJSON("'.JURI::root().'plugins/system/ajaxlogout/core/ajaxlogout.ajax.php",function(data){
						$("body").append(data.form);
						$("#logoutForm").submit();
					});
					
					if(typeof $(this).attr("href") == undefined || $(this).attr("href").length != 0) $(this).attr("href","javascript:;").removeClass("'.$class.'");
					return false;
				});
			});
		})(jQuery);		
		';
		$doc->addScriptDeclaration($js);
	}
}